package com.th.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.th.common.domain.UsUserAuthApply;
import com.th.common.enums.AuthType;
import com.th.common.enums.ExamineResultStatus;
import com.th.common.exception.BaseServiceException;
import com.th.common.mybatis.query.LambdaQueryWrapperX;
import com.th.common.vo.UserInfoVo;
import com.th.mapper.UsUserAuthApplyMapper;
import com.th.service.UsUserAuthApplyService;
import com.th.service.UsUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;

/**
 * @Author tuhang
 * @PackageName nswcy-crowdsource-client-server
 * @Package com.th.service.impl
 * @Date 2023/6/8 20:55
 * @Version 1.0
 */
@Service
public class UsUserAuthApplyServiceImpl
        extends ServiceImpl<UsUserAuthApplyMapper, UsUserAuthApply>
        implements UsUserAuthApplyService {


    @Resource
    private UsUserService userService;

    @Override
    public Boolean submitAuth(Integer authType, String data) {
        Long userId = userService.getCurrentUserId();
        UsUserAuthApply userAuthApply = this.getBaseMapper().selectOne(
                new LambdaQueryWrapperX<UsUserAuthApply>()
                        .eqIfPresent(UsUserAuthApply::getUserId, userId)
                        .eqIfPresent(UsUserAuthApply::getExamineResultStatus, ExamineResultStatus.UNREVIEW.getCode())
        );
        if(Objects.isNull(userAuthApply)){
            UsUserAuthApply newUserAuthApply = new UsUserAuthApply();
            newUserAuthApply.setUserId(userId);
            newUserAuthApply.setAuthType(authType);
            newUserAuthApply.setExamineResultStatus(ExamineResultStatus.UNREVIEW.getCode());
            newUserAuthApply.setExamineJson(data);
            newUserAuthApply.setApplyTime(LocalDateTime.now());
            int insert = this.getBaseMapper().insert(newUserAuthApply);
            //TODO 以短信的形式通知用户或者添加信息到站内信 通知用户

            return insert > 0;
        }else {
            throw new BaseServiceException(500, String.format("当前用户已经提交过 %s 的认证申请,请静待审核结果！！！", AuthType.of(userAuthApply.getAuthType()).getName()));
        }
    }

    @Override
    public UsUserAuthApply getAuthInfo(Integer authType) {
        Long userId = userService.getCurrentUserId();
        List<UsUserAuthApply> usUserAuthApplies = this.getBaseMapper().selectList(
                new LambdaQueryWrapperX<UsUserAuthApply>()
                        .eqIfPresent(UsUserAuthApply::getUserId, userId)
                        .eqIfPresent(UsUserAuthApply::getAuthType, authType)
                        .neIfPresent(UsUserAuthApply::getExamineResultStatus, ExamineResultStatus.UNPASS.getCode())
                        .orderByDesc(UsUserAuthApply::getApplyTime)
        );
         return CollectionUtils.isNotEmpty(usUserAuthApplies) ? usUserAuthApplies.get(0) : null;
    }
}
